package com.woniuxy.mq01.configuration;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @Author author
 * @Create 2023-10-07 10:08
 */
//@Component
public class ProducerCallBack implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //SpringBean的生命周期<bean id="" class="" init-method="" desotry-method=""/>
    //使用注解替代配置
    @PostConstruct
    public void setUp(){
        //监听要生效，必须在初始化是将当前监听类和rabbitTemplate关联在一起
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("cofirm");
        System.out.println("自定义消息id:correlationData:"+correlationData);
        System.out.println("是否进入交换机:ack:"+ack);
        System.out.println("cause:"+cause);
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("return");
        System.out.println("message:"+message);
        System.out.println("replyCode:"+replyCode);
        System.out.println("replyText:"+replyText);
        System.out.println("exchange:"+exchange);
        System.out.println("routingKey:"+routingKey);
    }
}
